// components/cart-item/CartItem.js
Component({
  options: {
    addGlobalClass: true
  },
  relations: {
    '../cart-action/CartAction': {
      type: 'parent',
      linked(parent) {
        this.parent = parent
      },
      unlinked () {
        this.parent = null
      }
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object,
      value: {}
    },
    currency: {
      type: String,
      value: '¥',
    },
  },
  observers: {
    item (val) {
      this.setData({
        checked: val.checked
      })
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    checked: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toggleChecked () {
      let checked = !this.data.checked
      this.setData({
        checked
      })
      this.triggerEvent('checked', {
        checked, 
        item: this.data.item
      })
    },

    increment () {
      this.triggerEvent('increment', this.data.item)
    },
    decrement () {
      this.triggerEvent('decrement', this.data.item)
    }
  }
})
